<template>
	
	<view>
		<map style="width:100%;height:60vh;"  :latitude="latitude" :longitude="longitude" :show-compass="true" :show-location="true"  @controltap="controltap" @regionchange="regionchange" id="map2"></map>
		<uni-card title="经度">{{longitude}}</uni-card>
		<uni-card title="纬度">{{latitude}}</uni-card>
		<button type="primary"  @click="checkHouses(0)">租房</button>
		<button type="warn"  @click="checkHouses(1)">买房</button>

	</view>
	 
</template>

<script>
	
	export default {
		data() {
			return {
					address:'',
					agentId:0,
					title: 'map', //地图标题
					latitude: 40.013305,  //纬度
					longitude: 118.685713,
					marker:[{//这里应该是可以写个marker的
						id:0,
						latitude:40.013305,
						longitude:118.685713}],
					
					StatusBar: this.StatusBar,
					CustomBar: this.CustomBar,
				
			}
		},
		onReady() {
		   let vm = this
		   vm.map = uni.createMapContext('map2', this)
		   vm.getLocation()
		  },
		
		methods: {
			//获取房源
			checkHouses(type){
				// 这里连后端
				const lon = this.longitude
				const lat = this.latitude
				if(type == 0){
					// 租房
					uni.navigateTo({
						url:'/pages/common/house_rent_in?lon='+lon+'&lat='+lat,
					})
				}
				else{
					// 买房
					uni.navigateTo({
						url:'/pages/common/house_buy?lon='+lon+'&lat='+lat,
					})
				}
			}
			,
			
			
		  //获取定位
		   getLocation() {
		    let vm = this
		    uni.getLocation({
		     type: 'gcj02',
		     geocode: true,//获取城市具体地址
		     success: (e) => {
		      if (e.address.city) {
			   vm.address = e.address
		       vm.cityName = e.address.city
		       vm.poiName = e.address.poiName
		       vm.moveToLocation()
		      }
		      vm.latitude = e.latitude
		      vm.longitude = e.longitude
			  // vm.updateLoc();
		      vm.run = 'start'
		      vm.addDingwei()
		     },
		     fail: (e) => {
			  console.log('fail')
		      vm.longitude = 113.953834 //e.longitude
		      vm.latitude = 22.540235 //e.latitude
		      vm.run = 'start'
		      vm.addDingwei()
		     },
		    })
		   },
		   //移动到指定位置
		   moveToLocation() {
		    let vm = this
		    let falg = Object.keys(vm.map).length
		    if (falg) {
		     vm.map.moveToLocation({
		      longitude: vm.longitude,
		      latitude: vm.latitude,
		      success: () => {}
		     })
		    }
		   },
		
		   //添加定位点
		   addDingwei() {
		    let vm = this,arr = [];
		    arr.push({
		     id: 0,
		     latitude: vm.latitude,
		     longitude: vm.longitude,
		     iconPath: '../../static/dingwei.png',
		     callout: {
		      content: '删除此站点', //文本
		      color: '#ffffff', //文字颜色
		      fontSize: 14, //文本大小
		      borderRadius: 2, //边框圆角
		      bgColor: '#00c16f', //背景颜色
		      display: 'BYCLICK', //点击显示
		     },
		    })
		    vm.markers = arr
		   },
		
		   //点击控件
		   controltap(e) {
		    let vm = this
		    if (e.detail.controlId === 0) {
		     vm.getLocation()
		    }
		    if (e.detail.controlId === 1) {
		     uni.chooseLocation({
		      success: (e) => {
		       vm.getLine(e)
		      }
		     })
		    }
		   },
		
		   //移动地图位置触发获取地图中心店坐标
		   regionchange(e) {
		    let vm = this
		    if (e.type != 'begin' && vm.run == 'start') {
					 console.log('change')
		     vm.map.getCenterLocation({
				 // c57eab66ecef610118acf5f0e13c1d82	
				 complete:function(e){
					 vm.longitude = e.longitude;
					 vm.latitude = e.latitude;
					 // vm.updateLoc();
				 },success:function(e){
					 vm.longitude = e.longitude;
					 vm.latitude = e.latitude;
					 // vm.updateLoc();
				 },fail:function(e){
					 vm.longitude = e.longitude;
					 vm.latitude = e.latitude;
					 // vm.updateLoc();
				 }
			 })
		    }
		   },
		   getCenterLocation(){
			   console.log('la')
			   
		   },
		   updateLoc(){
			   // 通过搞得地图的api更新位信息
			   let url = 'https://restapi.amap.com/v3/geocode/regeo?location='+this.longitude+
			   ','+this.latitude+'&key=c57eab66ecef610118acf5f0e13c1d82'
			   console.log(this.longitude)
			   let vm = this;
			   this.$axios.get(url).then(function(res){
				   console.log('address')
					 vm.address = res.responseJSON.regeocode.formatted_address
					 console.log(vm.address)
					 vm.city = res.responseJSON.regeocode.addressComponent.city;
					 vm.district = res.responseJSON.regeocode.addressComponent.district;
					 vm.street = res.responseJSON.regeocode.addressComponent.streetNumber.street
			   })
		   }
		   }
	}
</script>

<style>

</style>
